
#include "walker-control-v1.h"

namespace walker_control_v1
{
	class WalkerControlVars : public SessionSampleVars
	{
	public:
		SPADAS_VARS(WalkerControl, SessionSampleVars)
		WalkerControlData data;

		SessionGeneralSample toGeneralSample()
		{
			SessionGeneralSample sample;
			sample.timestamp() = timestamp;

			Array<GeneralElement> values(3);
			sample.values() = values;

			values[0] = data.requestControl ? 1 : 0;
			values[1] = data.heading;
			values[2] = data.speed;

			sample.significantCount() = values.size();
			return sample;
		}

		Bool fromSample(String protocol, SessionSample sample) override
		{
			if (!sample.is<SessionGeneralSample>()) return FALSE;

			auto generalSample = sample.as<SessionGeneralSample>();
			timestamp = generalSample.timestamp();
			auto values = generalSample.values();

			if (protocol == "walker-control-v1")
			{
				if (values.size() != 3) return FALSE;

				data.requestControl = values[0].valid && values[0].value == 1;
				data.heading = values[1].value;
				data.speed = values[2].value;

				return TRUE;
			}

			return FALSE;
		}
	};
}

using namespace walker_control_v1;

WalkerControl::WalkerControl() : Object<WalkerControlVars>(new WalkerControlVars, TRUE)
{}

FullTimestamp& WalkerControl::timestamp()
{
	return vars->timestamp;
}

WalkerControlData *WalkerControl::operator ->()
{
    return &vars->data;
}

String WalkerControl::getProtocolName(UInt channel)
{
    return (String)"walker-control-v1@" + channel;
}

SessionGeneralSample WalkerControl::toGeneralSample()
{
    return vars->toGeneralSample();
}

Array<String> WalkerControl::getProtocolNames(UInt channel)
{
	return Array<String>::create(1, "walker-control-v1").convert<String>([channel](auto& s){ return s + "@" + channel; });
}

Bool WalkerControl::fromGeneralSample(String protocol, SessionGeneralSample generalSample)
{
    return vars->fromSample(protocol, generalSample.as<SessionSample>());
}